package com.sc.ifs.spring;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.sc.util.adapter.ConfigBeanAdapter;
import com.sc.util.data.DataUtil;
import com.sc.util.data.MD5Util;
import com.sc.util.exception.ExceptionEnum;
/**
 * 
 * <h1>验证用户是否登录</h1> 
 * 
 * @author xujunbao 
 * @version LoginInteceptor.java,v0.1 2017年3月14日 上午10:15:36 xujunbao Exp  
 *
 */
@Aspect
@Component
public class LoginInteceptor {
	//判断是否登录的切点
	@Pointcut("@annotation(com.sc.ifs.spring.ValidateLogin)")
	public void validateAspect(){}
	
//	//判断是否登录的切点
//	@Pointcut("execution(* com.sc.ifs.member.AppMember*.*(..))")
//	public void validateAspect(){}
//	//判断时跳过银行卡管理请求切点
//	@Pointcut("!execution(* com.sc.ifs.member.AppMemberCardController.card_*(..))")
//	public void skipMemberCardLogin(){}
//	//判断时跳过登录请求切点
//	@Pointcut("!execution(* com.sc.ifs.member.AppMember*.login(..))")
//	public void skipValidateLogin(){}
//	//判断时跳过注册请求切点
//	@Pointcut("!execution(* com.sc.ifs.member.AppMember*.register*(..))")
//	public void SkipValidateRegister(){}
//	@Pointcut("!execution(* com.sc.ifs.member.AppMember*.findPwd(..))")
//	public void SkipValidateFindPwd(){}

	@SuppressWarnings("unchecked")
//	@Around(value = "validateAspect() and skipValidateLogin() and SkipValidateRegister() and SkipValidateFindPwd() and skipMemberCardLogin()")
	@Around(value = "validateAspect()")
	public Map<String, Object> validateLogin(ProceedingJoinPoint point){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 
		String time=request.getParameter("time");
		String mId=request.getParameter("mId");
		String token=request.getParameter("token");
		String validateKey=MD5Util.MD5(mId+time+ConfigBeanAdapter.privateKey);
		if(validateKey.equals(token)){
			try {
				resultMap=(Map<String, Object>) point.proceed();
				return resultMap;
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_USER_LOGIN);
			return resultMap;
		}
		return null;
	}
}
